package com.work.straighttalk.model;

import android.text.TextUtils;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.hyphenate.EMError;
import com.hyphenate.chat.EMChatManager;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.work.straighttalk.model.i.OnSMSFinishedListener;
import com.work.straighttalk.model.i.SMSMI;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;

/**
 * Created by 肖江宏 on 16/3/3.
 */
public class SMSMImpl implements SMSMI {

    /**
     * 验证手机格式
     *
     * @param username 手机号
     * @param listener 接口回调
     */
    @Override
    public void login(String username, final OnSMSFinishedListener listener) {
        boolean error = false;
        if (TextUtils.isEmpty(username)) {
            listener.onUsernameNull();
            error = true;
        }
        if (!error) {
            if (!isMatchLength(username, 11)
                    && !isMobileNO(username)) {
                listener.onUsernameError();
            } else {
                listener.onUsernameSuccess();
            }
        }
    }

    /**
     * 验证账号与密码,并注册账号
     *
     * @param username 账号
     * @param password 密码
     * @param listener 再一次输入密码
     */
    @Override
    public void login(final String username, final String password, final OnSMSFinishedListener listener) {
        boolean error = false;
        if (TextUtils.isEmpty(username)) {
            listener.onUsernameNull();
            error = true;
        }
        if (TextUtils.isEmpty(password)) {
            listener.onPasswordNull();
            error = true;
        }
        if (!error) {
            UserModel.getInstance().register(username, password, password, new LogInListener() {
                        @Override
                        public void done(Object o, BmobException e) {
                            if (e == null) {
                                listener.onSuccess();
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            EMClient.getInstance().createAccount(username, password);
                                        } catch (HyphenateException e1) {
                                            e1.printStackTrace();

//                                    DemoHelper.getInstance().setCurrentUserName(username);

                                            e1.printStackTrace();
                                            int errorCode = e1.getErrorCode();
                                            if (errorCode == EMError.NETWORK_ERROR) {
                                                Log.d("SMSMImpl", "errorCode:" + errorCode);
//                                        Toast.makeText(getApplicationContext(), getResources().getString(R.string.network_anomalies), Toast.LENGTH_SHORT).show();
                                            } else if (errorCode == EMError.USER_ALREADY_EXIST) {
                                                Log.d("SMSMImpl", "errorCode:" + errorCode);
//                                        Toast.makeText(getApplicationContext(), getResources().getString(R.string.User_already_exists), Toast.LENGTH_SHORT).show();
                                            } else if (errorCode == EMError.INVALID_USER_NAME) {
                                                Log.d("SMSMImpl", "errorCode:" + errorCode);
//                                        Toast.makeText(getApplicationContext(), getResources().getString(R.string.illegal_user_name),Toast.LENGTH_SHORT).show();
                                            } else {
                                                Log.d("SMSMImpl", "errorCode:" + errorCode);
//                                        Toast.makeText(getApplicationContext(), getResources().getString(R.string.Registration_failed) + e.getMessage(), Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    }
                                }).start();
                            } else {
                                if (e.getErrorCode() == BaseModel.CODE_NOT_EQUAL) {
                                }
                                listener.onError(e);
                            }
                        }
                    }
            );
        }
    }


    /**
     * 判断手机号码是否为空和长度
     *
     * @param str
     * @param length
     * @return
     */
    public static boolean isMatchLength(String str, int length) {
        if (str.isEmpty()) {
            return false;
        } else {
            return str.length() == length ? true : false;
        }
    }

    /**
     * 判断手机号码的格式是否正确
     */
    public static boolean isMobileNO(String mobileNums) {
        String telRegex = "[1][358]\\d{9}";
        if (TextUtils.isEmpty(mobileNums))
            return false;
        else
            return mobileNums.matches(telRegex);
    }


}

